
package com.oauth.eurekaoauthserver.service;


import com.oauth.eurekaoauthserver.common.PasswordEncoderEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.util.StringUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * @Auther: IceZhang
 * @Date: 2018/8/3 13:07
 * @Description:
 */

public class ClientDetailsServiceImpl implements ClientDetailsService {

    private Logger log = LoggerFactory.getLogger(ClientDetailsServiceImpl.class);
    
    public String selectClientDetailsSql = "select client_id, client_secret, resource_ids, scope, authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, autoapprove from oauth_client_details where client_id = ?";

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        ClientDetails details = null;
        try {
            details = (ClientDetails) jdbcTemplate.queryForObject(selectClientDetailsSql, new RowMapper<ClientDetails>() {
                @Override
                public ClientDetails mapRow(ResultSet rs, int rowNum) throws SQLException {
                    BaseClientDetails details = new BaseClientDetails(rs.getString(1), rs.getString(3), rs.getString(4), rs.getString(5),
                            rs.getString(7), rs.getString(6));
                    details.setClientSecret(PasswordEncoderEnum.equals.name() + ":" + rs.getString(2));// 文本比较密码
                    if (rs.getObject(8) != null) {
                        details.setAccessTokenValiditySeconds(Integer.valueOf(rs.getInt(8)));
                    }
                    if (rs.getObject(9) != null) {
                        details.setRefreshTokenValiditySeconds(Integer.valueOf(rs.getInt(9)));
                    }
                    String scopes = rs.getString(11);
                    if (scopes != null) {
                        details.setAutoApproveScopes(StringUtils.commaDelimitedListToSet(scopes));
                    }
                    return details;
                }
            }, new Object[] { clientId });
        } catch (EmptyResultDataAccessException e) {
            throw new NoSuchClientException("No client with requested id: " + clientId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("loadClientByClientId error", e);
            throw new ClientRegistrationException("Server ClientDetails error");
        }
        return details;
    }

}
